﻿
    using Chain.BLL;
//    using ChainStock.Controls;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public partial class RptStaffMoney : PageBase
    {
        private Staff bllStaff = new Staff();
        private StaffMoney bllStaffMoney = new StaffMoney();


        private void BindStaffMoneyList(string strSql)
        {
            int Counts = this.NetPagerParameter.RecordCount;
            strSql = strSql + " and Staff.StaffClassID = StaffClass.ClassID and StaffClass.ClassShopID = SysShop.ShopID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "ClassShopID", strSql);
            StringBuilder strSb = new StringBuilder();
            strSb.Append(" 1=1 ");
            if (this.txtStaffStartTime.Value != "")
            {
                strSb.AppendFormat(" and StaffMoney.StaffCreateTime>='{0}'", DateTime.Parse(this.txtStaffStartTime.Value));
            }
            if (this.txtStaffEndTime.Value != "")
            {
                strSb.AppendFormat(" and StaffMoney.StaffCreateTime<='{0}'", DateTime.Parse(this.txtStaffEndTime.Value + " 23:59:59"));
            }
            DataTable dtStaff = this.bllStaff.GetListSP(this.NetPagerParameter.PageSize, this.NetPagerParameter.CurrentPageIndex, out Counts, strSb.ToString(), new string[] { strSql }).Tables[0];
            this.NetPagerParameter.RecordCount = Counts;
            this.NetPagerParameter.CustomInfoHTML = string.Format("<div class=\"results\"><span>当前第{0}/{1}页 共{2}条记录 每页{3}条</span></div>", new object[] { this.NetPagerParameter.CurrentPageIndex, this.NetPagerParameter.PageCount, this.NetPagerParameter.RecordCount, this.NetPagerParameter.PageSize });
            this.rptStaffMoneyList.DataSource = dtStaff;
            this.rptStaffMoneyList.DataBind();
            PageBase.BindSerialRepeater(this.rptStaffMoneyList, this.NetPagerParameter.PageSize * (this.NetPagerParameter.CurrentPageIndex - 1));
        }

        private void BindStaffTotalMoney()
        {
            StaffMoney bllStaffMoney = new StaffMoney();
            string strSql = this.QueryCondition() + " and Staff.StaffClassID = StaffClass.ClassID and StaffClass.ClassShopID = SysShop.ShopID and StaffMoney.StaffID = Staff.StaffID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "ClassShopID", strSql);
            this.lblStaffMoney.Text = bllStaffMoney.GetStaffTotalMoney(strSql).ToString("0.00");
        }

        protected void btnRptStaffMoneyExcel_Click(object sender, EventArgs e)
        {
            int Counts = this.NetPagerParameter.RecordCount;
            string strSql = this.QueryCondition();
            StringBuilder strDetail = new StringBuilder();
            strSql = strSql + " and Staff.StaffClassID = StaffClass.ClassID and StaffClass.ClassShopID = SysShop.ShopID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "ClassShopID", strSql);
            StringBuilder strSb = new StringBuilder();
            strSb.Append(" 1=1 ");
            if (this.txtStaffStartTime.Value != "")
            {
                strSb.AppendFormat(" and StaffMoney.StaffCreateTime>='{0}'", DateTime.Parse(this.txtStaffStartTime.Value));
                strDetail.AppendFormat(" and  StaffCreateTime>='{0}'", DateTime.Parse(this.txtStaffStartTime.Value));
            }
            if (this.txtStaffEndTime.Value != "")
            {
                strSb.AppendFormat(" and StaffMoney.StaffCreateTime<='{0}'", PubFunction.TimeEndDay(this.txtStaffEndTime.Value));
                strDetail.AppendFormat(" and StaffCreateTime<='{0}'", PubFunction.TimeEndDay(this.txtStaffEndTime.Value));
            }
            DataTable dtStaff = this.bllStaff.GetListSP(0x186a0, 1, out Counts, strSb.ToString(), new string[] { strSql }).Tables[0];
            DataExcelInfo.StaffMoney(dtStaff, base._UserName, strDetail.ToString());
        }

        protected void btnRptStaffMoneyQuery_Click(object sender, EventArgs e)
        {
            this.BindStaffTotalMoney();
            this.BindStaffMoneyList(this.QueryCondition());
        }

        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.NetPagerParameter.PageSize = int.Parse(this.drpPageSize.SelectedValue);
            this.BindStaffTotalMoney();
            this.BindStaffMoneyList(this.QueryCondition());
        }

        protected void NetPagerParameter_PageChanging(object src, PageChangingEventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = e.NewPageIndex;
            this.BindStaffTotalMoney();
            this.BindStaffMoneyList(this.QueryCondition());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                PubFunction.BindShopSelect(base._UserShopID, this.sltStaffShopID, true);
                PubFunction.BindShopSelect(base._UserShopID, this.sltShop, true);
                PubFunction.BindStaffClass(base._UserShopID, this.sltStaffClassID, true);
                this.txtStaffStartTime.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtStaffEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                if ((PubFunction.curParameter.dataAuthority != 0) && (base._UserShopID > 1))
                {
                    this.sltStaffShopID.Value = this._UserShopID.ToString();
                    this.sltStaffShopID.Attributes.Add("disabled", "disabled");
                }
                this.txtMemStartTime.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtMemEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.BindStaffTotalMoney();
                this.BindStaffMoneyList(this.QueryCondition());
            }
        }

        protected string QueryCondition()
        {
            string strQueryStaff = PubFunction.RemoveSpace(this.txtQueryStaff.Value);
            string strStaffClass = this.sltStaffClassID.Value;
            string strStaffShop = this.sltStaffShopID.Value;
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" 1=1");
            if (strQueryStaff != "")
            {
                strSql.AppendFormat(" and (StaffName like '%{0}%' or StaffNumber = '{0}' or StaffMobile = '{0}')", strQueryStaff);
            }
            if (strStaffClass != "")
            {
                strSql.AppendFormat(" and StaffClassID ={0}", int.Parse(strStaffClass));
            }
            if (strStaffShop != "")
            {
                strSql.AppendFormat(" and ShopID={0}", int.Parse(strStaffShop));
            }
            return strSql.ToString();
        }

        protected void rptStaffMoneyList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                DataRowView dr = (DataRowView) e.Item.DataItem;
                Repeater rptItem = (Repeater) e.Item.FindControl("rptStaffMoneyDetail");
                if (rptItem != null)
                {
                    int recordCount = this.NetPagerParameter.RecordCount;
                    string strSql = " StaffID=" + dr["StaffID"].ToString();
                    StringBuilder strSb = new StringBuilder();
                    strSb.Append(" and 1=1 ");
                    if (this.txtStaffStartTime.Value != "")
                    {
                        strSb.AppendFormat(" and StaffCreateTime>='{0}'", DateTime.Parse(this.txtStaffStartTime.Value));
                    }
                    if (this.txtStaffEndTime.Value != "")
                    {
                        strSb.AppendFormat(" and StaffCreateTime<='{0}'", DateTime.Parse(this.txtStaffEndTime.Value + " 23:59:59"));
                    }
                    strSql = strSql + strSb;
                    DataTable dt = this.bllStaffMoney.GetListSP(strSql).Tables[0];
                    rptItem.DataSource = dt;
                    rptItem.DataBind();
                    foreach (RepeaterItem rp in rptItem.Items)
                    {
                        Label lblDetailNum = (Label) rp.FindControl("lblDetailNumber");
                        lblDetailNum.Text = (rp.ItemIndex + 1).ToString();
                    }
                }
            }
        }
    }


